package com.foreknow.service.impl;

import com.foreknow.dao.DepartmentMapper;
import com.foreknow.entity.Constantitem;
import com.foreknow.entity.Department;
import com.foreknow.service.DepartmentService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(Department record) {
        return 0;
    }

    @Override
    public List<Department> selectAllDepartmentByCondition(Integer deptType) {
        return departmentMapper.selectAllDepartmentByCondition(deptType);
    }

    @Override
    public Department selectByPrimaryKey(Integer id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> selectAllDepartment() {
        return departmentMapper.selectAllDepartment();
    }

    @Override
    public int updateByPrimaryKeySelective(Department record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Department record) {
        return 0;
    }
    @Override
    public List<Department> queryDeptName() {
        return departmentMapper.queryDeptNameArr();
    }

    @Override
    public List<Constantitem> queryItem(int id) {
        return departmentMapper.queryItem(id);
    }

    @Override
    public int deleteDepById(int id) {
        return departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertDep(Department department) {
        return departmentMapper.insert(department);
    }

    @Override
    public int updateDep(Department department) {
        return departmentMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public List<Department> selectByWhere(Department department) {
        return departmentMapper.selectDep(department);
    }

    @Override
    public Department seletById(int id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int excelToList(InputStream inputStream) throws IOException,  org.apache.poi.openxml4j.exceptions.InvalidFormatException {

        List<Department> gisList = new ArrayList<>();

        Workbook workbook = null;
        workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        //工作表对象
        Sheet sheet = workbook.getSheetAt(0);
        //总行数
        int rowLength = sheet.getLastRowNum() + 1;
        //工作表的列
        Row row = sheet.getRow(0);
        //总列数
        int colLength = row.getLastCellNum();
        for (int i = 2; i < rowLength; i++) {
            row = sheet.getRow(i);
            row.getCell(0).setCellType(CellType.STRING);
            row.getCell(1).setCellType(CellType.STRING);
            row.getCell(2).setCellType(CellType.STRING);
            row.getCell(3).setCellType(CellType.STRING);
            row.getCell(4).setCellType(CellType.STRING);

            String cell0 = row.getCell(0).getStringCellValue().trim();;
            if(cell0==""){
                break;
            }
            String cell1 = row.getCell(1).getStringCellValue().trim();
            String cell2 = row.getCell(2).getStringCellValue().trim();
            Integer cell3 = Integer.parseInt(row.getCell(3).getStringCellValue().trim());
            Integer cell4 = Integer.parseInt(row.getCell(4).getStringCellValue().trim());
            Department department = new Department();
            department.setDeptcode(cell1);
            department.setDeptname(cell2);
            department.setDeptcategoryid(cell3);
            department.setDepttype(cell4);
            department.setDelmark(1);
            gisList.add(department);
        }
        return departmentMapper.insertMultiGis(gisList);
    }
}
